package com.example.fish.controller;

import com.example.fish.pojo.Race;
import com.example.fish.pojo.User;
import com.example.fish.service.RaceService;
import com.example.fish.service.impl.CompetitionService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.Top3Info;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/race")
public class RaceController extends BaseController{
    @Autowired
    private RaceService raceService;
    @Autowired
    private CompetitionService competitionService;

//    定时任务  --- 得到剩余时间
    @GetMapping("/schedule")
    private JsonResult<Long> schedule(){
        long time = competitionService.getTime();
        return new JsonResult<>(200,"success",time);
    }

    /***
     * 参赛
     */
    @PostMapping("/add")
    private JsonResult<Void> add(String summary,MultipartFile file, HttpSession session){
        User user =(User) session.getAttribute("user");
        raceService.addRace(user.getUserid(), summary,file);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/getSelection")
    private JsonResult<PageInfo<Race>> getSelection(Integer pageNum){
        PageInfo<Race> raceList = raceService.getRaceList(pageNum);
        return new JsonResult<>(200,"success",raceList);
    }
    @PostMapping("/getNextSelection")
    private JsonResult<PageInfo<Race>> getNextSelection(Integer pageNum){
        PageInfo<Race> raceList = raceService.getNextRaceList(pageNum);
        return new JsonResult<>(200,"success",raceList);
    }

    @PostMapping("/isRaced")
    private JsonResult<Boolean> israced(Integer raceid, HttpSession session){
        User user =(User) session.getAttribute("user");
        Boolean select = raceService.select(user.getUserid(), raceid);
        return new JsonResult<>(200,"success",select);
    }
    @PostMapping("/vote")
    private JsonResult<Void> vote(Integer raceid, HttpSession session){
        User user =(User) session.getAttribute("user");
        raceService.vote(user.getUserid(), raceid);
        return new JsonResult<>(200,"success",null);
    }

    @GetMapping("/getVoteCount")
    private JsonResult<Integer> getCount(){
        Integer voteCount = raceService.getVoteCount();
        return new JsonResult<>(200,"success",voteCount);
    }
    @GetMapping("/getTop3")
    private JsonResult<List<Top3Info>> getTop3(){
        List<Top3Info> top3 = raceService.getTop3();
        return new JsonResult<>(200,"success",top3);
    }
}
